package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.goods.entity.goods.GoodsCategorySpec;
import com.hoshiicloud.goods.entity.goods.GoodsSpecifications;
import com.hoshiicloud.goods.entity.goods.GoodsSpecificationsValues;
import com.hoshiicloud.goods.mapper.goods.GoodsCategorySpecMapper;
import com.hoshiicloud.goods.service.GoodsCategorySpecService;
import com.hoshiicloud.goods.service.GoodsSpecificationsService;
import com.hoshiicloud.goods.service.GoodsSpecificationsValuesService;
import com.hoshiicloud.goods.vo.GoodsSpecificationsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类目规格
 * @author linjx on 2019/7/25
 */
@Slf4j
@Service
public class GoodsCategorySpecServiceImpl  extends ServiceImpl<GoodsCategorySpecMapper, GoodsCategorySpec> implements GoodsCategorySpecService {

    @Autowired
    private GoodsSpecificationsService goodsSpecificationsService;
    @Autowired
    private GoodsSpecificationsValuesService goodsSpecificationsValuesService;

    /**
     * 根据类目id, 回去该类目的所有规格 和 每个规格下的所有规格值
     */
    @Override
    public List<GoodsSpecificationsVo> getProperSpecByCategoryId(Long id) {

        List<GoodsSpecificationsVo> propList = new ArrayList<>();

        // 根据类目id，查询该类目下的 规格id， GoodsCategorySpec 类目规格
        QueryWrapper<GoodsCategorySpec> wrapper = new QueryWrapper<>();
        wrapper.lambda().select(GoodsCategorySpec::getSpecId);
        wrapper.lambda().eq(GoodsCategorySpec::getCategoryId, id)
                .eq(GoodsCategorySpec::getDeleted, 0);
        List<GoodsCategorySpec> lists = baseMapper.selectList(wrapper);

        if (lists.isEmpty()) {
            return new ArrayList<>();
        }
        // 添加规格信息
        lists.stream().forEach(entity ->{
            // 查询规格信息
            GoodsSpecificationsVo specVo = goodsSpecificationsService.getSpecById(entity.getSpecId());
            // 设置规格值信息
            specVo.setSpecificationsValuesList(goodsSpecificationsValuesService.getSpecById(entity.getSpecId()));
            propList.add(specVo);
                });
        return propList;
    }

    @Override
    @Cacheable(cacheNames = "GoodsCategorySpec", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#categoryId")
    @Logable(authName = "商品管理-商品类目规格表管理-获取某商品类目下的规格")
    public List<GoodsSpecifications> listByCategoryId(Long categoryId) {
        return baseMapper.listGoodsCategorySpecByCategoryId(categoryId);
    }

    @Override
    @CacheEvict(cacheNames = "GoodsCategorySpec", allEntries = true)
    @Logable(authName = "商品管理-商品类目规格表管理-批量删除某商品类目下的规格")
    public Integer deleteByCategoryId(Long categoryId) {
        return baseMapper.deleteByCategoryId(categoryId);
    }

    @Override
    @CacheEvict(cacheNames = "GoodsCategorySpec", allEntries = true)
    @Logable(authName = "商品管理-商品类目规格表管理-批量保存某商品类目下的规格")
    public Boolean saveBatch1(List<GoodsCategorySpec> list) {
        return this.saveBatch(list);
    }
}
